package br.com.lis;

import java.util.ArrayList;
import java.util.HashMap;

import br.com.lis.data.DatabaseOperations;
import android.content.Context;
import android.database.Cursor;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class AulaAdapter extends BaseAdapter {
	private Context context;
	private ArrayList<Aula> lista;
	
	
	DatabaseOperations dop;
	String nomeUsuario;
	SessionManager session;
	String anteriorConcluidaString;
	Cursor cr;
	String curso;
	String c1aul1,c1aul2,c1aul3,c1aul4,c2aul1,c2aul2;
	
	
	public AulaAdapter(Context context, ArrayList<Aula> lista){
		this.context = context;
		this.lista = lista;
	}
	
	public void updateResults(ArrayList<Aula> results) {
		 lista = results;
	        //Triggers the list update
	        notifyDataSetChanged();
	}

	@Override
	public int getCount() {
		return lista.size();
	}

	@Override
	public Object getItem(int position) {
		return lista.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		Aula aula = lista.get(position);
		View layout;
		
		if(convertView == null){
			LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			layout = inflater.inflate(R.layout.aulas, null);
		}
		else{
			layout = convertView;
		}
		

		TextView nome = (TextView) layout.findViewById(R.id.tvNomeAula);
		nome.setText(aula.getNome());
		
		TextView descricao = (TextView) layout.findViewById(R.id.tvDescrAula);
		descricao.setText(aula.getDescricao());
		ImageView ivCheck = (ImageView) layout.findViewById(R.id.ivChecked);
		ivCheck.setBackgroundResource(R.drawable.checked_green);
		ivCheck.setVisibility(View.INVISIBLE);

		
		if(position % 2 == 0){
			layout.setBackgroundColor(Color.parseColor("#ffffff"));
			nome.setTextColor(Color.parseColor("#800080"));
			descricao.setTextColor(Color.parseColor("#DDA0DD"));
			ivCheck.setBackgroundResource(R.drawable.checked_white);

		}
		
		// Session class instance
	    session = new SessionManager(context);
	    
		// get user data from session
        HashMap<String, String> user = session.getUserDetails();       
        // name
        nomeUsuario = user.get(SessionManager.KEY_NAME);
		
		dop = new DatabaseOperations(context);
		if(nomeUsuario!=null){
			cr = dop.getAulaAnteriorConcluida(dop, nomeUsuario);
	   		cr.moveToFirst();
	   		
	   	    c1aul1 =  cr.getString(1);   
	   	    c1aul2 =  cr.getString(2);    
	   	    c1aul3 =  cr.getString(3);    
	   	    c1aul4 =  cr.getString(4);    
	   	    c2aul1=  cr.getString(5);    
	        c2aul2=  cr.getString(6);      		
	    	cr.close();  
	    	dop.close();
		}
		
		if(curso.equals("1")){
			if(position == 0){
				if (c1aul1!=null){
					ivCheck.setVisibility(View.VISIBLE);
				}
			}
			if(position == 1){
				if (c1aul2!=null){
					ivCheck.setVisibility(View.VISIBLE);
				}
			}
			if(position == 2){
				if (c1aul3!=null){
					ivCheck.setVisibility(View.VISIBLE);
				}
			}
			if(position == 3){
				if (c1aul4!=null){
					ivCheck.setVisibility(View.VISIBLE);
				}
			}
			
		}
		else if(curso.equals("2")){
			if(position == 0){
				if (c2aul1!=null){
					ivCheck.setVisibility(View.VISIBLE);
				}
			}
			if(position == 1){
				if (c2aul2!=null){
					ivCheck.setVisibility(View.VISIBLE);
				}
			}
		}
		
		
		/*if(position == 0 && curso.equals("1")){
			if (c1aul1==null){
				ivCheck.setVisibility(View.INVISIBLE);
			}
		}
		
		
		if(position == 1 && curso.equals("1")){
			if (c1aul2==null){	
				ivCheck.setVisibility(View.INVISIBLE);
			}
		}
		
		if(position == 2 && curso.equals("1")){
			if (c1aul3==null){
				ivCheck.setVisibility(View.INVISIBLE);
			}
		}
		
		if(position == 3 && curso.equals("1")){
			if (c1aul4==null){		
				ivCheck.setVisibility(View.INVISIBLE);
			}
		}
		
		if(position == 0 && curso.equals("2")){
			if (c2aul1==null){	
				ivCheck.setVisibility(View.INVISIBLE);
			}
		}
		
		if(position == 1 && curso.equals("2")){
			if (c2aul2==null){
				ivCheck.setVisibility(View.INVISIBLE);
			}
		}*/
		
		return layout;
	}

	public void setCurso(String cURSO) {
        this.curso = cURSO;
    }
	
}
